package com.LatitudeYao;

/**
 * Created by GTS on 2017/8/21.
 */
public class NumberOfOne {
    public  int countFunction1(int n ){//朴素方法计算n，根据位数进行循环
//        int count = 0;
//        int flag = 1;
//        int n1= n;
//
//        while (true){
//            if(n1 & flag){
//                count ++;
//            }
//            flag = flag<<1;
//        }
//        return count;

        int count = 0;
        while(n != 0){
            count++;
            n = (n-1) & n;
        }
        return count;
    }
    public static void main(String[] args) {
        NumberOfOne test = new NumberOfOne();
        int i = test.countFunction1(10);
        System.out.println(i);
    }
}
